package com.syni.mdd.yhd.common.all.listen.video;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.*;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import com.syni.mdd.yhd.common.all.utils.TokenManager;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className 视频发布监听(类似本地消息队列)
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/7 15:44
 * @VERSION 1.0
 **/
@Component
public class VideoReleaseEventListener implements ApplicationListener<VideoEvent> {

    final static String MODEL = "videoRelease";

    @Autowired
    BmsUserBusinessService bmsUserBusinessService;

    @Autowired
    BmsDxNewsService bmsDxNewsService;

    @Autowired
    BmsUserBusinessAuthService bmsUserBusinessAuthService;

    @Autowired
    BmsMusicService bmsMusicService;

    @Override
    @Async
    public void onApplicationEvent(VideoEvent videoEvent) {
        String model = videoEvent.getSource().toString();
        if(!MODEL.equals(model)){
            return;
        }
        BmsUserVideo bmsUserVideo = videoEvent.getBmsUserVideo();
        BmsVideo bmsVideo = videoEvent.getBmsVideo();
        BmsDxNews bmsDxNews = new BmsDxNews();
        bmsDxNews.setIsDelete(Constants.COMMON_FLAG_FALSE_INT);
        if(videoEvent.getVideoReleaseType() == VideoEvent.USER_RELEASE){
            //往店铺新增条发布视频通知(用户发布)
            bmsDxNews.setBmsBusinessId(bmsVideo.getBmsId());
            bmsDxNews.setOpUserId(bmsUserVideo.getUserId());
            bmsDxNews.setBmsVideoId(bmsVideo.getId());
            bmsDxNews.setParentType(Constants.PARENTTYPT_INT);
            bmsDxNews.setChildType(Constants.PARENTTYPT_INT_RELEASE_VIDEO);
            bmsDxNews.setVideoImg(bmsVideo.getCloudFileSnapshot10Url());
            bmsDxNews.setVideoName(bmsVideo.getVendorName());
            BmsUser bmsUser = TokenManager.getUser(bmsUserVideo.getUserId());
            if(bmsUser != null) {
                bmsDxNews.setTitile(bmsUser.getLoginName());
            }
            bmsDxNews.setOriginRole(Constants.BUSINESS_ROLE_ID);
            bmsDxNewsService.save(bmsDxNews);
            //进行推送
            /*String alert = String.format(AuroraPushCode.pushTemplateMap.get(AuroraPushCode.ASSISTANT_VIDEO_RELEASE).getAlert(),
                    bmsDxNews.getTitile(),videoReleaseTime,videoDesc);
            BmsCommonService.pushToFront(bmsDxNews,AuroraPushCode.ASSISTANT_VIDEO_RELEASE,alert);*/
        }
        else if(videoEvent.getVideoReleaseType() == VideoEvent.ASSISTANT_RELEASE){
            //往店铺新增条发布视频通知(店员发布)
            //bmsDxNews.setBmsUserId();
            bmsDxNews.setBmsBusinessId(bmsVideo.getBmsId());
            bmsDxNews.setBmsUserId(bmsUserVideo.getUserId());
            bmsDxNews.setOpUserId(bmsUserVideo.getUserId());
            bmsDxNews.setBmsVideoId(bmsVideo.getId());
            bmsDxNews.setParentType(Constants.PARENTTYPT_SYS);
            bmsDxNews.setChildType(Constants.PARENTTYPT_SYS_ASS_REA_INFORM_BUS);
            List<BmsUserBusiness> bmsUserBusiness = bmsUserBusinessService.getBaseDao().findByBmsUserIdAndBmsBusinessId(bmsUserVideo.getUserId(),
                    bmsVideo.getBmsId());
            bmsDxNews.setTitile(bmsUserBusiness.get(0).getNameInStore());
            bmsDxNews.setStatus(Constants.COMMON_FLAG_FALSE_INT);
            //bmsDxNews.setFromOrigin(Constants.BUSINESS_CLIENT);
            bmsDxNews.setOriginRole(Constants.BUSINESS_ROLE_ID);
            Map<String,Object> userDataMap = new HashMap<>();
            String videoReleaseTime = DateTimeUtils.converDateToString(bmsVideo.getNewTime(),DateTimeUtils.DATE_PATTERN_DAY_05);
            String videoDesc = "";
            if(StringUtils.isNotBlank(bmsVideo.getVideoDescribe())){
                Integer startIndex = 0;
                Integer endIndex = 20;
                if(bmsVideo.getVideoDescribe().length() < 20){
                    endIndex = bmsVideo.getVideoDescribe().length();
                }
                videoDesc = bmsVideo.getVideoDescribe().substring(startIndex,endIndex);
            }
            userDataMap.put("videoReleaseTime", videoReleaseTime);
            userDataMap.put("videoDesc",videoDesc);
            bmsDxNews.setContent(JSON.toJSONString(userDataMap));
            String alert = String.format(AuroraPushCode.pushTemplateMap.get(AuroraPushCode.ASSISTANT_VIDEO_RELEASE).getAlert(),
                    bmsDxNews.getTitile(),videoReleaseTime,videoDesc);
            BmsCommonService.pushToFront(bmsDxNews,AuroraPushCode.ASSISTANT_VIDEO_RELEASE,alert);
            bmsDxNewsService.save(bmsDxNews);
        }
        //统计音乐
        if(StringUtils.checkNullAndEmpty(videoEvent.getUserData().get("musicId"))) {
            Integer musicId = Integer.parseInt(videoEvent.getUserData().get("musicId").toString());
            countMusic(musicId);
        }
    }

    public void countMusic(Integer musicId){
        if(musicId != null){
            BmsMusic bmsMusic = bmsMusicService.getBaseDao().findByIdAndIsDelete(musicId,Constants.COMMON_FLAG_FALSE_INT);
            if(bmsMusic != null){
                bmsMusic.setUseTotal(bmsMusic.getUseTotal() == null ? 1 : bmsMusic.getUseTotal() + 1);
                bmsMusicService.save(bmsMusic);
            }
        }
    }
}
